﻿using DataCenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UserCenter;

namespace GRPA
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 全局变量，用于存储api地址
        /// </summary>
        public static string Api = "http://192.168.113.128:8080";
        public static MissionCenter.MissionCenterPanel missionCenterPanel = null;//new MissionCenter.MissionCenterPanel(api);
        public static Workbench.WorkbenchPanel workbenchPanel = null;//new Workbench.WorkbenchPanel(api);
        public static DataCenter.DataCenterPanel dataCenterPanel = null;//new DataCenter.DataCenterPanel();
        public static StudyCenter.StudyCenterPanel studyCenterPanel = null;//new StudyCenter.StudyCenterPanel();
        public static MessageCenter.MessageCenterPanel messageCenterPanel = null;//new MessageCenter.MessageCenterPanel();
        public static UserCenter.UserCenterPanel userCenterPanel = new UserCenter.UserCenterPanel(Api);


        public MainWindow()
        {
            InitializeComponent();
            
            //订阅登录成功事件
            UserCenterPanel.LoginSuccessEvent += UserCenterPanel_LoginSuccessEvent;

            /*1.先要处理登录的问题，登录成功后才能进入主界面
             * 2.主界面的加载，需要根据用户的权限来加载不同的界面
             * 
             * 处理登录的问题:自动登录，记住密码，登录后的界面
             * 需要勾选才可以自动登录，自动登录后，下次打开软件就不需要登录了
             * 
             * 
             * 检查是否自动登录,如果是自动登录，就尝试登录，如果登录成功，就进入主界面,如果登录失败，就进入登录界面
             */

            Loaded += MainWindowLoadedInitialize;
        }

        /// <summary>
        /// 加载完毕后,初始化主界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindowLoadedInitialize(object sender, RoutedEventArgs e)
        {
            // 在 Loaded 事件中自动触发按钮点击事件
            userCenter_Click(userCenter, new RoutedEventArgs());
        }
        private void UserCenterPanel_LoginSuccessEvent(object sender, bool e)
        {


            //如果登录成功，就进入主界面
            //如果登录失败，就锁定在登录界面
            if (e)
            {

                Console.WriteLine("登录成功.......................token:  " + UserCenterPanel.Token);
                //本地数据库初始化
                localDataBaseInitial();


                //登录成功后，就可以加载主界面了
                missionCenterPanel = new MissionCenter.MissionCenterPanel(Api,UserCenterPanel.Token,UserCenterPanel.UserInfor);
                workbenchPanel = new Workbench.WorkbenchPanel(Api, UserCenterPanel.Token, UserCenterPanel.UserInfor);
                dataCenterPanel = new DataCenter.DataCenterPanel(Api, UserCenterPanel.Token, UserCenterPanel.UserInfor);
                studyCenterPanel = new StudyCenter.StudyCenterPanel(Api, UserCenterPanel.Token, UserCenterPanel.UserInfor);
                messageCenterPanel = new MessageCenter.MessageCenterPanel(Api, UserCenterPanel.Token, UserCenterPanel.UserInfor);


                main_banner.IsEnabled = true;
                //默认打开任务中心
                banner_mission_Click(banner_mission, new RoutedEventArgs());

            }
            else
            {
                main_banner.IsEnabled = false;
            }

        }

        private void localDataBaseInitial()
        {
            Console.WriteLine(".................................................");
            LocalDataManage.scriptLocalDataBase($"users/{UserCenterPanel.UserInfor["username"]}/userdata.db");
            LocalDataManage.missionLocalDataBase($"users/{UserCenterPanel.UserInfor["username"]}/userdata.db");
            //LocalDataManage.scriptLocalWrite($"users/{UserCenterPanel.UserInfor["username"]}/userdata.db");
            LocalDataManage.missionLocalWrite($"users/{UserCenterPanel.UserInfor["username"]}/userdata.db");

            Console.WriteLine(UserCenter.UserCenterPanel.UserInfor);
            Console.WriteLine(UserCenterPanel.Token);
            Console.WriteLine(Api);
            Console.WriteLine(".................................................");

        }



        /// <summary>
        /// 窗口拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();

        }

        private void MaxZ_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                wsmaxz.Content = "▣";
                WindowState = WindowState.Maximized;

            }
            else
            {
                wsmaxz.Content = "☐";
                WindowState = WindowState.Normal;

            }

        }

        private void Min_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;

        }


        /// <summary>
        /// 任务中心加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void banner_mission_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));
            main_workBench.Children.Clear();
            main_workBench.Children.Add(missionCenterPanel);
        }

        /// <summary>
        /// 创建中心加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void banner_workbench_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));

            main_workBench.Children.Clear();
            main_workBench.Children.Add(workbenchPanel);
        }


        /// <summary>
        /// 数据管理加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void banner_dataCenter_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));

            main_workBench.Children.Clear();
            main_workBench.Children.Add(dataCenterPanel);
        }

        /// <summary>
        /// 用户中心加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userCenter_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));

            main_workBench.Children.Clear();
            main_workBench.Children.Add(userCenterPanel);

        }

        /// <summary>
        /// 消息中心加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void information_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));

            main_workBench.Children.Clear();
            main_workBench.Children.Add(messageCenterPanel);
        }

        /// <summary>
        /// 学习中心加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void studyGRHA_Click(object sender, RoutedEventArgs e)
        {
            ElemendClickChangeColor(sender as Button, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8100")));
            main_workBench.Children.Clear();
            main_workBench.Children.Add(studyCenterPanel);
        }



        /// <summary>
        /// 唯一选中按钮
        /// </summary>
        private class OnlyOneSelectedButton
        {
            public Button OldButton { get; set; }
            public SolidColorBrush OldColor { get; set; }
        }

        private OnlyOneSelectedButton onlyOneSelectedButton = new OnlyOneSelectedButton();

        private void ElemendClickChangeColor(Button x, SolidColorBrush y)
        {

            if (onlyOneSelectedButton.OldButton != null)
            {
                // 恢复旧按钮的原来字体颜色
                onlyOneSelectedButton.OldButton.Foreground = onlyOneSelectedButton.OldColor;
            }
            // 保存x的字体颜色
            onlyOneSelectedButton.OldColor = (SolidColorBrush)x.Foreground;

            // 修改当前按钮的字体颜色
            x.Foreground = y;

            // 保存当前按钮为旧按钮
            onlyOneSelectedButton.OldButton = x;

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //如果是最大化
            if (WindowState == WindowState.Maximized)
            {
                mainGrid.Width = this.Width;
                mainGrid.Height = this.Height;
            }
            else
            {
                mainGrid.Width = this.Width - 8;
                mainGrid.Height = this.Height - 8;

            }
        }
    }
}
